function parseTime(time, fmt) {
    var t = new Date();
    if (!time)
        return '--';
    t.setTime(time);
    var o = {
        "M+": t.getMonth() + 1, //月份
        "d+": t.getDate(), //日
        "h+": t.getHours(), //小时
        "m+": t.getMinutes(), //分
        "s+": t.getSeconds(), //秒
        "q+": Math.floor((t.getMonth() + 3) / 3), //季度
        "S": t.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (t.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

var GlacierCalendar = {
    dateFormat: "yyyy/MM/dd",
    eventMap: new Map(),
    initCalendar: function (hiddenId, selectCallBack) {
        var eventsData = [];
        var data = $("#" + hiddenId).val();
        data.split(",").forEach(function (item) {
            if (item) {
                eventsData.push({title: "", editable: false, start: item});
                GlacierCalendar.eventMap.set(item, "1");
            }
        });
        $('#calendar').fullCalendar({
            editable: true,
            events: eventsData,
            eventColor: '#378006',
            selectable: true,
            selectHelper: true,
            select: function (start, end) {
                var formatStart = parseTime(start, GlacierCalendar.dateFormat);
                var formatEnd = parseTime(end, GlacierCalendar.dateFormat);
                if (formatStart != formatEnd) {
                    var tmp = new Date(start);
                    while (true) {
                        if (tmp > end) {
                            break;
                        }
                        var formatTmp = parseTime(tmp, GlacierCalendar.dateFormat);
                        if (GlacierCalendar.eventMap.get(formatTmp)) {
                            tmp.setDate(tmp.getDate() + 1);
                            continue;
                        }

                        var eventData = {title: "", start: formatTmp, editable: false};
                        $('#calendar').fullCalendar('renderEvent', eventData, true);
                        GlacierCalendar.eventMap.set(formatTmp, "1");
                        tmp.setDate(tmp.getDate() + 1);
                    }
                    GlacierCalendar.flush(hiddenId);
                    if(selectCallBack){
                        selectCallBack();
                    }
                    return;
                }
                if (GlacierCalendar.eventMap.get(formatStart)) {
                    $('#calendar').fullCalendar('removeEvents', function (event) {
                        return parseTime(event.start, GlacierCalendar.dateFormat) == formatStart;
                    });
                    GlacierCalendar.eventMap.delete(formatStart);
                } else {
                    var eventData = {
                        title: "",
                        start: formatStart,
                        editable: false
                    };
                    $('#calendar').fullCalendar('renderEvent', eventData, true);
                    GlacierCalendar.eventMap.set(formatStart, "1");
                }
                GlacierCalendar.flush(hiddenId);
                if(selectCallBack){
                    selectCallBack();
                }
                $('#calendar').fullCalendar('unselect');
            }
        });
    },
    flush: function (hiddenId) {
        var tmp = [];
        GlacierCalendar.eventMap.forEach(function (v, k) {
            tmp.push(k);
        });
        $("#" + hiddenId).val(tmp.join(","));
    }
};